package com.example.guessNumber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class GuessNumberActivity extends ListActivity {
	
	final static int GIVEN_GUESSES = 10;
	final static int MAX_NUM = 1000;
	final static int MIN_NUM = 1;
	
	List<Map<String, String>> guesses = new ArrayList<Map<String, String>>();
	String[] from = new String[]{"guess", "comment"};
	int[] to = new int[]{R.id.number, R.id.hint};
	
	EditText guess;
	TextView leftGuesses;
	Button makeGuess;
	String leftGuessesText;
	String high, low, win, lose;
	
	int leftGuessesCount;
	int mysteriousNumber;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        guess = (EditText) findViewById(R.id.guess);
        leftGuesses = (TextView) findViewById(R.id.left_guesses);
        makeGuess = (Button) findViewById(R.id.make_guess);
        leftGuessesText = getString(R.string.left_guesses);
        high = getString(R.string.high);
        low = getString(R.string.low);
        win = getString(R.string.win);
        lose = getString(R.string.lose);
        
        leftGuessesCount = GIVEN_GUESSES; 
        mysteriousNumber = (int) (Math.random() * (MAX_NUM - MIN_NUM) + MIN_NUM);
        
        makeGuess.setOnClickListener(new OnClickListener(){
        	public void onClick(View v){
        		handleGuess();
        	}
        });
        
//        guess.setOnEditorActionListener(new OnEditorActionListener() {
//			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//				handleGuess();
//				return true;
//			}
//		});
        
        aggiornaUI();
        
    }
    
    private void handleGuess(){
    	leftGuessesCount--;
    	
    	Integer number = Integer.parseInt(guess.getText().toString());
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("guess", number.toString());
    	
    	if (number == mysteriousNumber){
    		map.put("comment", win);
    		makeGuess.setEnabled(false);
    	}
    	else if (leftGuessesCount == 0){
    		map.put("comment", lose+" "+mysteriousNumber);
    	}
    	else{
	    	if (number > mysteriousNumber)
	    		map.put("comment", high);
	    	else
	    		map.put("comment", low);
    	}
    	
    	guesses.add(0, map);
    	
    	aggiornaUI();
    	
    }
    
    private void aggiornaUI(){
    	ListAdapter adapter = new SimpleAdapter(this, guesses, R.layout.row, from, to);
        setListAdapter(adapter);
        
        leftGuesses.setText(leftGuessesCount + " " + leftGuessesText);
        
        guess.setText("");
    }
    

}